package com.blink.optimize.apis.collect;

import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

/**
 * <Description> 集合优化</Description>
 * <ClassName> TestCollectionOptimitize</ClassName>
 *
 * @Author liuxianzhao
 * @Date 2018年01月08日 14:24
 */
public class TestCollectionOptimitize {
    //1.迭代器模式
    //2.流，并行流
    //3.Lamda表达式

    @Test
    public void testJava7StatsticLongWords() {
        try {
            String contents = new String(Files.readAllBytes(Paths.get("F:\\private_code\\gitee\\optimitize\\pom.xml")), StandardCharsets.UTF_8);
            List<String> stringList = Arrays.asList(contents.split("\n"));
            int count = 0;
            for (String s : stringList) {
                if (s.length() > 12) {
                    count++;
                }
            }
            System.out.println(count);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testJava8StreamStatsticLongWords() {
        try {
            String contents = new String(Files.readAllBytes(Paths.get("F:\\private_code\\gitee\\optimitize\\pom.xml")), StandardCharsets.UTF_8);
            List<String> stringList = Arrays.asList(contents.split("\n"));
            long count = stringList.stream().filter(s -> s.length() > 12).count();
            System.out.println(count);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void testJava8ParallelStreamStatsticLongWords() {
        try {
            String contents = new String(Files.readAllBytes(Paths.get("F:\\private_code\\gitee\\optimitize\\pom.xml")), StandardCharsets.UTF_8);
            List<String> stringList = Arrays.asList(contents.split("\n"));
            long count = stringList.parallelStream().filter(s -> s.length() > 12).count();
            System.out.println(count);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
